package net.huashitong.node.web;

import java.util.*;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import freemarker.ext.beans.HashAdapter;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.dao.NodeNewtempletDao;
import net.huashitong.node.entity.NodeNewtemplet;
import net.huashitong.node.filter.NodeNewtempletFilter;
import net.huashitong.node.service.NodeNewtempletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;

import net.huashitong.Project.filter.ProjectFilter;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.entity.NodeSchedules;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.node.service.NodeSchedulesService;
import net.huashitong.user.filter.BnpUserFilter;

@Controller
@RequestMapping(value = "api/nodes/")
public class NodesController extends JsonBaseController {

    @Autowired
    private NodeSchedulesService service;
    @Autowired
    private NodeNewschedulesService nss;
    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private NodeNewtempletService nodeNewtempletService;

    @Override
    protected String getView(String view) {
        return null;
    }

    /*
     * 查询全部节点
     */
    @ResponseBody
    @RequestMapping(value = "queryNodeSchedules")
    public Message queryNodes(@RequestBody ProjectFilter filter) {
        List<NodeSchedules> list = service.queryNodeSchedules();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("nodesList", list);
        return coverMessage("200", "失败", map);
    }

    /**
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryNodesList")
    public Message queryNodesList(@RequestBody BnpUserFilter filter) {
        List<NodeNewschedules> list = nss.queryListByNameAndPhone(filter);
        return coverMessage("200", "获取成功", list);
    }

    /**
     * 获取重大项目总的节点数
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getdatacount")
    public Message getDataCount() {
        int count = nss.getDataCount();
        return coverMessage("200", "获取成功", count);
    }

    /**
     * 节点时间更正
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "nodeTimeCorrectionsStart")
    public Message nodeTimeCorrections() {
        //查找出所有节点
        List<NodeNewschedules> nodeNewschedules = nss.query();
        List<NewProjects> newProjects = newProjectsService.query();
        NodeNewtempletFilter filter = new NodeNewtempletFilter();
        Map<String,List<NodeNewtemplet>> tempMap = new TreeMap<>();
        //查询节点模板
        for (int i = 0; i < 3; i++) {
            filter.setNodeType(i+"");
            List<NodeNewtemplet> templets = nodeNewtempletService.queryNodeTempletInfo(filter);
            tempMap.put(i+"",templets);
        }
        for (NewProjects project : newProjects) {
            //节点Id----节点计划时间
            List<Map<String, NodeNewschedules>> list = new ArrayList<>();
            Map<String, NodeNewschedules> map = new TreeMap<>();
            for (NodeNewschedules node : nodeNewschedules) {
                if (project.getSysId().equals(node.getProjectId())) {
                    if (!map.containsKey(node.getNodeId())) {
                        map.put(node.getNodeId(), node);
                        list.add(map);
                    }
                }
            }
            //Integer.parseInt(StringUtils.isBlank(node.getPlanTime())?"0":node.getPlanTime())
            String natureType = project.getNatureType();
            if("3".equals(natureType)||"4".equals(natureType)){
                natureType = "0";
            }
            List<NodeNewtemplet> tempList = tempMap.get(natureType);

            int sumPlanTime = 0;
            List<NodeNewschedules> resList = new ArrayList<>();
            for(NodeNewtemplet temp : tempList){
                //相应节点的计划时间
                NodeNewschedules nodePlanTime = map.get(temp.getNodeId());

                //大节点
                if("0".equals(temp.getNodeIds())){
                    nodePlanTime.setPlanTime(sumPlanTime+"");
                    sumPlanTime = 0;
                    resList.add(nodePlanTime);
                }else{
                    //小节点确定是需要
                    if("1".equals(StringUtils.isBlank(nodePlanTime.getState())?"0":nodePlanTime.getState())){
                        //小节点
                        int planTime = Integer.parseInt(StringUtils.isBlank(nodePlanTime.getPlanTime())?"0":nodePlanTime.getPlanTime());
                        sumPlanTime += planTime;
                    }
                }
            }
            nss.batchUpdate(resList);

        }
        return coverMessage("200", "获取成功","计划时间更新成功");

    }

    @ResponseBody
    @RequestMapping(value = "checkNodeIsFinished",method = RequestMethod.GET)
    public Message checkNodeIsFinished(String nodeSysId){
        NodeNewschedules nodeNewschedules = nss.getById(nodeSysId);
        if(ObjectUtils.isNotBlank(nodeNewschedules)){
            //已完成
            if("2".equals(nodeNewschedules.getStatus())){
                return coverMessage("200", "节点已完成",true);
            }else{
                return coverMessage("200", "节点未完成",false);
            }
        }else{
            return coverMessage("500", "节点未完成",false);
        }
    }
}
